//定义一个小仓库pinia
import { defineStore } from 'pinia';
//引入数据泛型
import type { categoryStoreStateType } from './interface/category';
//引入请求函数
import { reqC1List, reqC2List,reqC3List } from '@/api/product/attr/attr';

let useCategoryStore = defineStore('category', {
    //存储数据的地方
    state: ():categoryStoreStateType => {

        return {
            //存储一级分类的数据
            c1List: [],
            //存储一级分类的数据
            c2List: [],
            //存储一级分类的数据
            c3List: [],
            //存储一级分类的ID
            c1Id: '',
            //存储一级分类的ID
            c2Id: '',
            //存储一级分类的ID
            c3Id: ''
        }
    },
    //处理业务的地方
    actions: {
        async getC1List() {
            //获取一级分类的数据
            let result = await reqC1List();
            this.c1List = result;
        },
        //一级分类下拉菜单触发change事件，获取二级分类的数据
        async getC2List() {
            //清空二级、三级分类数据
            this.c2Id = '';
            this.c3Id = '';

            this.c2List = [];
            this.c3List = [];

            let result = await reqC2List(this.c1Id);
            this.c2List = result;
        },
        async getC3List() {
            //清空三级分类数据
            this.c3Id = '';

            this.c3List = [];

            let result = await reqC3List(this.c2Id);
            this.c3List = result;
        }
    }
});
//对外暴露
export default useCategoryStore;